/**
 * @Description
 * @author 20155870 杨润增
 * @create 2018-05-06 9:55
 **/
package com.campus_information_portal.dao;

import com.campus_information_portal.entity.DorStuTerm;
import com.campus_information_portal.entity.DormitoryApplyForm;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author 20155870 杨润增
 * @className DormitorySqlProvider
 * @Description
 * @create 2018-05-06 9:55
 **/
public class DormitorySqlProvider {
    public String query(DormitoryApplyForm dormitoryApplyForm) {
        StringBuilder sql = new StringBuilder("select * from dormitory_apply_form where 1=1");
        if (dormitoryApplyForm.getStuInfoId() != null) {
            sql.append(" and stu_info_id=#{stuInfoId}");
        }
        if (dormitoryApplyForm.getDormitoryNum() != null) {
            sql.append(" and dormitory_num=#{dormitoryNum}");
        }
        return sql.toString();
    }

    public String queryApplyByConditions(final DorStuTerm dorStuTerm) {
        return new SQL() {{
            SELECT("dormitory_apply_form.*");
            SELECT("student.stu_num ,student.stu_name,student.academy_id,student.stu_sex");
            SELECT("term.term_num,term.term_year");
            SELECT("academy.academy_name");
            FROM("dormitory_apply_form ,student,term,academy");
            WHERE("dormitory_apply_form.stu_info_id=student.stu_info_id");
            WHERE("dormitory_apply_form.term_id=term.term_id");
            WHERE("student.academy_id=academy.academy_id");
            if (dorStuTerm.getStuName() != null) {
                WHERE("stu_name=#{stuName}");
            }
            if (dorStuTerm.getDormitoryNum() != null) {
                WHERE("dormitory_num=#{dormitoryNum}");
            }
            if (dorStuTerm.getAcademyName() != null) {
                WHERE("academy_name=#{academyName}");
            }
            if (dorStuTerm.getStuNum() != null) {
                WHERE("stu_num=#{stuNum}");
            }
            if (dorStuTerm.getStuSex() != null) {
                WHERE("stu_sex=#{stuSex}");
            }
            if (dorStuTerm.getApplyState() != null) {
                WHERE("apply_state=#{applyState}");
            }
            if (dorStuTerm.getTermYear() != null) {
                WHERE("term_year=#{termYear}");
            }
            if (dorStuTerm.getTermNum() != null) {
                WHERE("term_num=#{termNum}");
            }
            if (dorStuTerm.getApplyDate() != null) {
                WHERE("apply_date=#{applyDate}");
            }

        }}.toString();
    }
}
